import json
import requests
from aiogram import Bot, Dispatcher, types, F
import asyncio
import aiofiles
import io
import logging
import datetime
import time
from asgiref.sync import sync_to_async, async_to_sync
from icecream import ic
from numpy import number
# from models import get_users, init, get_user, create_user, create_conteiner, get_first_conteiner, ImagesConteiner, get_conteiner, StatusConteiner
from aiogram.filters import CommandStart, Command
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove, Message, ContentType, InputMediaPhoto, InputFile
from aiogram.fsm.context import FSMContext
from app.keyboards import cont_info_kb, go_back, go_error
# from utilites.utilits_bot import coincidence
from aiogram import Router
# from app.keyboards import 
from form.forms import Taktik
from utilites.main2 import cont_infos, detect_re_number, script_run
import tempfile


router = Router()


@router.message(CommandStart())
async def start(message: types.Message, state: FSMContext):
    await message.delete()
    await state.set_state(Taktik.one)
    text = f'Привет, {message.from_user.full_name}!\n'\
        f'Для получения ПСО пришлите список контейнеров'
    await message.answer(text, reply_markup=cont_info_kb)
    

@router.callback_query()
async def filter_action(callback: types.CallbackQuery, state: FSMContext):
    if callback.data == 'go_back':
        await callback.answer()
        await state.set_state(Taktik.one)
        await callback.message.edit_text('Для получения ПСО пришлите список контейнеров', reply_markup=cont_info_kb)
    elif callback.data == 'pso_info':
        await callback.answer()
        await state.set_state(Taktik.two)
        await two_action(callback, state)
    elif callback.data == 'info_cont':
        await callback.answer()
        await state.set_state(Taktik.three)
        await cont_info(callback, state)


async def two_action(callback: types.CallbackQuery, state: FSMContext):
    await callback.answer()
    await state.set_state(Taktik.two)
    bot_message =await callback.message.edit_text('Отправьте номера контейнеров списком', reply_markup=go_back)
    await state.update_data({'bot_message_id': bot_message.message_id})
    await state.set_state(Taktik.three)


async def cont_info(callback: types.CallbackQuery, state: FSMContext):
    await callback.answer()
    await state.set_state(Taktik.two)
    bot_message =await callback.message.edit_text('Отправьте номер контейнера', reply_markup=go_back)
    await state.update_data({'bot_message_id': bot_message.message_id})
    await state.set_state(Taktik.four)


async def filtering_text(message: Message, state: FSMContext):
    if await detect_re_number(message.text):
        return True
    else:
        await message.delete()
          
        
@router.message(Taktik.three, filtering_text)
async def three_action(message: types.Message, state: FSMContext):
    await message.delete()
    cont_list = await detect_re_number(message.text)
    await script_run(cont_list, message, state)
    await state.set_state(Taktik.one)
    await message.answer('Документы учета отправлены', reply_markup=go_back)
    

@router.message(Taktik.four, filtering_text)
async def three_action(message: types.Message, state: FSMContext):
    await message.delete()
    cont_list = await detect_re_number(message.text)
    text_message = await cont_infos(cont_list[0])
    await state.set_state(Taktik.one)
    await message.answer(text_message, reply_markup=go_back, parse_mode='html')
